<?php
    include("/connect.php");
    $partial = $_POST['partialFriend'];
    if($partial != ""){
        echo "<table>";
        $query1="SELECT DISTINCT First_Name, Last_Name, SID
            FROM user
            LEFT JOIN friends AS f
            ON (SID=f.friend1 OR SID=f.friend2) 
                AND ((f.friend1=$id OR f.friend1 IS NULL) OR (f.friend2=$id OR f.friend2 IS NULL))
            WHERE (f.friend1 IS NULL OR f.friend2 IS NULL)  AND First_Name LIKE '%$partial%'
            ORDER BY Last_Name;";
        $result1 = mysqli_query($con, $query1);
        $num=  mysqli_fetch_lengths($result1);
        $echoed = false;
        while($row1 = mysqli_fetch_assoc($result1)) {
                $sid = $row1['SID'];
                $f=$row1['First_Name'];
                $l=$row1['Last_Name'];
                $size=20;
                
                $userGenQuery="SELECT gender FROM user WHERE sid=$id";
                $rowUserGen= mysqli_fetch_assoc(mysqli_query($con, $userGenQuery));
                $genU = $rowUserGen['gender'];
                
                $friendGenQuery="SELECT gender FROM user WHERE sid=$sid";
                $rowFriendGen= mysqli_fetch_assoc(mysqli_query($con, $friendGenQuery));
                $genF= $rowFriendGen['gender'];
                
                if($genF===$genU){
                    echo "<tr><td id=$sid>$f $l <a href=\"#\" onclick=\"addFriend($sid);\">
                        <img src='../images/invite.png' width='".$size."px' height='".$size."px'></a></td></tr>";
                    $echoed=true;
                }
                
            }
            if(!$echoed){
                     echo "<tr><td>No Results</td></tr>";
                    }
        echo "</table>";
        }
    else {
        echo "<table><tr><td>No Results</td></tr></table>";
    }
    mysqli_close($con);
?>